package Game

import scala.io.StdIn
import scala.util.Random

object Game extends App {
  val AI = Map("1"->"刘备","2"->"关羽","3"->"张飞")

  val method = Map("1"->"石头","2"->"剪刀","3"->"布")

  var player:user=null

  var cpu:computer=null

  var round=0

  game

  class user(name:String){
    var playerName=name
    var score=0
    var wins=0
    var looses=0
    var draws=0

    def showFirst():String ={
      var in = StdIn.readLine()
      if(!method.keySet.contains(in)){
        println("输出不符合规范，默认出布")
        in = "3"
      }
      println(s"你出拳：${method(in)}")
      in
    }

  }

  class computer(name:String){
    var playerName=name
    var score=0
    var wins=0
    var looses=0
    var draws=0

    def showFirst():String ={
      var reslut= Random.nextInt(3)+1
      println(s"${cpu.playerName}出拳：${method(reslut.toString)}")
      reslut.toString
    }

  }

  def rules(playerRe:String,cpuRe:String)={
    var draw= playerRe==cpuRe
    val win = (playerRe == "1" && cpuRe == "2") ||
      (playerRe == "2" && cpuRe =="3") || (playerRe == "3" && cpuRe == "1")

    if(draw){
      player.draws+=1
      player.score+=1
      cpu.draws +=1
      cpu.score+=1
      println("平手")
    }else if(win){
      player.wins+=1
      player.score+=3
      cpu.looses +=1
      cpu.score-=1
      println("你赢了")
    }else{
      cpu.wins+=1
      cpu.score+=3
      player.looses+=1
      player.score-=1
      println("你输了")
    }
  }
  def init={
    println("--------欢迎进入游戏世界--------")
    println("****************************")
    println("**********猜拳开始！**********")
    println("****************************\n\n")

    println("请输入你的名字")
    val name = StdIn.readLine()
    player = new user(name)
    println("请选择对战角色：（1.刘备 2.关羽 3.张飞）")
    val cpuName = StdIn.readLine()
    cpu = new computer(AI(cpuName))
    println(s"你选择了与${cpu.playerName}对战")
  }

  def game={
    init
    var isContinue=true
    println("要开始么？y/n")
    var in=StdIn.readLine()
    do{
      in match {
        case "y" =>{
          println("请出拳！1.石头 2.剪刀 3.布")
          var playerRe = player.showFirst()
          println(s"${cpu.playerName}出拳！")
          var cpuRe = cpu.showFirst()

          rules(playerRe,cpuRe)
          round +=1

          println("是否开始下一轮？（y/n）")
          in=StdIn.readLine()
        }
        case "n"=>{
          isContinue = false
          println("退出游戏！")
          println("--------------------------------")
          println(s"${player.playerName}与${cpu.playerName}对战${round}局")
          println("姓名\t得分\t胜局\t和局\t负局")
          println(s"${player.playerName}\t${player.score}\t${player.wins}\t${player.draws}\t${player.looses}")
          println(s"${cpu.playerName}\t${cpu.score}\t${cpu.wins}\t${cpu.draws}\t${cpu.looses}")

        }
        case _ =>{
          println("输入异常！是否开始下一轮？（y/n）")
          in=StdIn.readLine()
        }
      }
    }while(isContinue)
  }

}
